<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput
					v-model="phoneData"
					type="text"
					maxlength="11"
					placeholder="用户名/电话"
				></wInput>
				<wInput
					v-model="passData"
					type="password"
					maxlength="11"
					placeholder="密码"
				></wInput>
			</view>
			<wButton 
				text="登 录"
				:rotate="isRotate" 
				@click.native="startLogin()"
			></wButton>
			
			<!-- 其他登录 -->
			<view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-weibo" @tap="login_weibo"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-github" @tap="login_github"></view>
				</view>
			</view>
			
			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	var _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				phoneData:'', //用户/电话
				passData:'', //密码
				isRotate: false, //是否加载旋转
			};
		},
		components:{
			wInput,
			wButton,
		},
		mounted() {
			_this= this;
			// this.isLogin();
		},
		methods: {
			// isLogin(){
			// 	// 判断缓存中是否登录过，直接登录
			// 	try {
			// 		const value = uni.getStorageSync('setUserData');
			// 		if (value) {
			// 			//有登录信息
			// 			console.log("已登录用户：",value);
			// 			_this.$store.dispatch("setUserData",value); //存入状态
			// 			uni.reLaunch({
			// 				url: '../../../pages/index',
			// 			});
			// 		}
			// 	} catch (e) {
			// 		// error
			// 	}
			// },
		    startLogin(){
				//登录
				//登录界面跳转需要强制跳转
				// uni.reLaunch({
				// 	url: '../longIndex/longIndex'
				// });
				if(this.phoneData=='admin'&&this.passData=='admin'){
					uni.reLaunch({
						url: '../admin/admin'
					});
					
				}
				
				
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '用户名不能为空'
				    });
				    return;
				}
		        if (this.passData.length < 5) {
		            uni.showToast({
		                icon: 'none',
						position: 'bottom',
		                title: '密码不正确'
		            });
		            return;
		        }
				
				
				
				_this.isRotate=true
				setTimeout(function(){
					_this.isRotate=false
				},3000)
			
				uni.showLoading({
					title: '登录中'
				});

				uni.request({
					url: 'http://wd1900144.pro.wdcase.com/carApi/user.php',
					method: 'POST',
					data: {
						act:'login',
						phone:_this.phoneData,
						password:_this.passData
					},
					header:{
						'content-type':'application/x-www-form-urlencoded'
					},
					success: res => {
						console.log("登录成功")
						console.log(res.data.uinfo);
						if(res.data.error==0){
							let userdata={
								"name":res.data.uinfo.name,
								"phone":res.data.uinfo.phone,
								"id":res.data.uinfo.uid,
								"credit":res.data.uinfo.credit
								// "accesstoken":res.data.accesstoken,
							} //保存用户信息和accesstoken安全令牌
							// _this.$store.dispatch("setUserData",userdata); //存入状态
							try {
								uni.setStorageSync('setUserData', userdata); //存入缓存
							} catch (e) {
								// error
							}
							uni.showToast({
								icon: 'success',
								position: 'bottom',
								title: '登录成功'
							});
							uni.reLaunch({
								url: '../longIndex/longIndex'
							});
						}else{
							_this.passData=""
							if(_this.phoneData!='admin')
							{uni.showToast({
								icon: 'error',
								position: 'bottom',
								title: '账号或密码错误'
							});}
						}
						uni.hideLoading();
						
					},
					fail: () => {uni.hideLoading();},
					complete: () => {}
				});
				
				
			
				
		    }
			,
		
			//微信授权app登录
			//to login Adroid
			login_weixin() {
				let that=this;
				uni.login({
				provider: 'weixin',	
				success: loginRes => {
					//获取用户基本信息
					  uni.getUserInfo({
							provider: 'weixin',
							success: function(infoRes) {
							console.log(infoRes.rawData.name);
						 } ,
						fail: function (err) {
							// 登录失败 
							 console.log(err);
						}  
					})
							
					}
			})
			},
			
			login_weibo() {
				//微博登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_github() {
				//github登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
